package compoundDB.database.container;

import static org.junit.Assert.*;
import org.junit.Test;

import compoundDB.database.enumeration.DataType;

/**
 * This class tests the SearchOption class.
 * 
 * @author Kohl Bromwich
 */
public class SearchOptionTest
{
	@Test
	public void testCreateSearchRange()
	{
		// Test it works
		ColumnField col = new ColumnField("testColumn", DataType.FLOAT, 5);
		SearchOption so = SearchOption.createSearchRange(col, 30, 50);
		assertTrue(so.getParsedExpression().equals("testColumn >= 30 && testColumn <= 50"));
		
		// Test it fails
		ColumnField col2 = new ColumnField("testColumn2", DataType.STRING, 7);
		try
		{
			SearchOption.createSearchRange(col2, 20, 40);
			fail("Above create call should have thrown an exception due to incorrect data type of column field");
		}catch(IllegalArgumentException exc){}
	}
	
	@Test
	public void testCreateSearchMin()
	{
		// Test it works
		ColumnField col = new ColumnField("testColumn", DataType.INTEGER, 5);
		SearchOption so = SearchOption.createSearchMin(col, 30);
		assertTrue(so.getParsedExpression().equals("testColumn >= 30"));
		
		// Test it fails
		ColumnField col2 = new ColumnField("testColumn2", DataType.BYTES, 7);
		try
		{
			SearchOption.createSearchMin(col2, 20);
			fail("Above create call should have thrown an exception due to incorrect data type of column field");
		}catch(IllegalArgumentException exc){}
	}
	
	@Test
	public void testCreateSearchMax()
	{
		// Test it works
		ColumnField col = new ColumnField("testColumn", DataType.LONG, 5);
		SearchOption so = SearchOption.createSearchMax(col, 50);
		assertTrue(so.getParsedExpression().equals("testColumn <= 50"));
		
		// Test it fails
		ColumnField col2 = new ColumnField("testColumn2", DataType.STRING, 7);
		try
		{
			SearchOption.createSearchMax(col2, 40);
			fail("Above create call should have thrown an exception due to incorrect data type of column field");
		}catch(IllegalArgumentException exc){}
	}
	
	@Test
	public void testCreateSearchEqualNum()
	{
		// Test it works
		ColumnField col = new ColumnField("testColumn", DataType.DOUBLE, 5);
		SearchOption so = SearchOption.createSearchEqual(col, 50);
		assertTrue(so.getParsedExpression().equals("testColumn = 50"));
		
		// Test it fails
		ColumnField col2 = new ColumnField("testColumn2", DataType.STRING, 7);
		try
		{
			SearchOption.createSearchEqual(col2, 40);
			fail("Above create call should have thrown an exception due to incorrect data type of column field");
		}catch(IllegalArgumentException exc){}
	}
	
	@Test
	public void testCreateSearchEqualStr()
	{
		// Test it works
		ColumnField col = new ColumnField("testColumn", DataType.STRING, 5);
		SearchOption so = SearchOption.createSearchEqual(col, "poison");
		assertTrue(so.getParsedExpression().equals("testColumn = 'poison'"));
		
		// Test it fails
		ColumnField col2 = new ColumnField("testColumn2", DataType.LONG, 7);
		try
		{
			SearchOption.createSearchEqual(col2, "poisonous");
			fail("Above create call should have thrown an exception due to incorrect data type of column field");
		}catch(IllegalArgumentException exc){}
	}
	
	@Test
	public void testCreateSearchContains()
	{
		// Test it works
		ColumnField col = new ColumnField("testColumn", DataType.STRING, 5);
		SearchOption so = SearchOption.createSearchContains(col, "is");
		assertTrue(so.getParsedExpression().equals("testColumn LIKE '%is%'"));
		
		// Test it fails
		ColumnField col2 = new ColumnField("testColumn2", DataType.BYTES, 7);
		try
		{
			SearchOption.createSearchContains(col2, "ise");
			fail("Above create call should have thrown an exception due to incorrect data type of column field");
		}catch(IllegalArgumentException exc){}
	}
	
	@Test
	public void testCreateSearchStartsWith()
	{
		// Test it works
		ColumnField col = new ColumnField("testColumn", DataType.STRING, 5);
		SearchOption so = SearchOption.createSearchStartsWith(col, "poi");
		assertTrue(so.getParsedExpression().equals("testColumn LIKE 'poi%'"));
		
		// Test it fails
		ColumnField col2 = new ColumnField("testColumn2", DataType.DOUBLE, 7);
		try
		{
			SearchOption.createSearchStartsWith(col2, "po");
			fail("Above create call should have thrown an exception due to incorrect data type of column field");
		}catch(IllegalArgumentException exc){}
	}
	
	@Test
	public void testCreateSearchEndsWith()
	{
		// Test it works
		ColumnField col = new ColumnField("testColumn", DataType.STRING, 5);
		SearchOption so = SearchOption.createSearchEndsWith(col, "son");
		assertTrue(so.getParsedExpression().equals("testColumn LIKE '%son'"));
		
		// Test it fails
		ColumnField col2 = new ColumnField("testColumn2", DataType.LONG, 7);
		try
		{
			SearchOption.createSearchEndsWith(col2, "on");
			fail("Above create call should have thrown an exception due to incorrect data type of column field");
		}catch(IllegalArgumentException exc){}
	}
}
